package cn.ulc.socket.admin.service.goods.impl;

import cn.ulc.common.constant.enums.AvailableStatus;
import cn.ulc.common.constant.enums.DeletedStatus;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.socket.admin.common.BaseService;
import cn.ulc.socket.admin.model.dto.goods.GoodsUnitDTO;
import cn.ulc.socket.admin.model.dto.goods.GoodsUnitListDTO;
import cn.ulc.socket.admin.model.param.goods.GoodsUnitSaveParam;
import cn.ulc.socket.admin.service.goods.GoodsUnitServ;
import cn.ulc.store.dao.goods.GoodsDao;
import cn.ulc.store.dao.goods.GoodsTypeDao;
import cn.ulc.store.dao.goods.GoodsUnitDao;
import cn.ulc.store.entity.goods.GoodsUnit;
import cn.ulc.store.entity.staff.Staff;
import lombok.AllArgsConstructor;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class GoodsUnitServImpl extends BaseService implements GoodsUnitServ {
    GoodsDao goodsDao;
    GoodsTypeDao typeDao;
    GoodsUnitDao unitDao;

    @Override
    public GoodsUnitListDTO findList(String name) {
        Staff loginUser = getCurrentUser();
        List<GoodsUnit> unitList = unitDao.findList(name,loginUser.getCompanyId());
        return new GoodsUnitListDTO(unitList);
    }
    @Override
    public GoodsUnitDTO findDetail(Long id) {
        GoodsUnit terminalGoodsUnit = unitDao.findOne(id);
        if(terminalGoodsUnit == null) throw new PlatformException("获取单位信息失败!");
        return new GoodsUnitDTO(terminalGoodsUnit);
    }

    @Override
    public Boolean save(GoodsUnitSaveParam param) {
        Staff loginUser = getCurrentUser();
        GoodsUnit unit = new GoodsUnit();
        unit.setCompanyId(loginUser.getCompanyId());
        unit.setName(param.getName());
        unit.setMode(param.getMode());
        Integer sort = unitDao.findMaxSort(loginUser.getCompanyId());
        if(sort==null)  sort = 0;
        unit.setSort(sort+1);
        unit.setStatus(AvailableStatus.AVAILABLE);
        unit.setCompanyId(loginUser.getCompanyId());
        setBaseInfo(unit);
        unitDao.save(unit);
        return true;
    }

    @Override
    public Boolean update(Long id, GoodsUnitSaveParam param) {
        GoodsUnit unit = unitDao.findOne(id);
        if(unit == null) throw new PlatformException("获取单位信息失败!");
        unit.setName(param.getName());
        unit.setMode(param.getMode());
        setBaseInfo(unit);
        unitDao.save(unit);
        return true;
    }

    @Override
    public Boolean deleted(Long id) {
        GoodsUnit unit = unitDao.findOne(id);
        if(unit == null) throw new PlatformException("获取单位信息失败!");
        unit.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(unit);
        unitDao.save(unit);
        return true;
    }

    @Override
    public Boolean changeStatus(Long id, AvailableStatus status) {
        GoodsUnit unit = unitDao.findOne(id);
        if(unit == null) throw new PlatformException("获取单位信息失败!");
        if(status == AvailableStatus.FREEZE){  //禁用
            Long i =  goodsDao.findCountByUnit(unit.getId());
            if(i >0 )throw new PlatformException("单位已被使用，无法禁用!");
        }
        unit.setStatus(status);
        setBaseInfo(unit);
        unitDao.save(unit);
        return true;
    }

    @Override
    public Boolean changeSort(String type, Long id1, Long id2) {
        Staff loginUser = getCurrentUser();
        GoodsUnit goodsUnit = unitDao.findOne(id1);
        if(goodsUnit==null) throw new PlatformException("操作失败!");
        if("down".equals(type)){   //下移
            GoodsUnit goodsType2 = unitDao.findOne(id2);
            List<GoodsUnit> goodsTypeList = unitDao.findBySort(goodsUnit.getSort(),goodsType2.getSort(),loginUser.getCompanyId());
            for(GoodsUnit t:goodsTypeList){
                if(t.getId().longValue() != goodsUnit.getId().longValue()){
                    t.setSort(t.getSort()-1);
                }else{
                    t.setSort(goodsType2.getSort());
                }
            }
            unitDao.saveAll(goodsTypeList);
        }else{  //上移 或 置顶
            Integer begin = 1;
            if("up".equals(type)){   //上移
                GoodsUnit goodsUnit2 = unitDao.findOne(id2);
                if(goodsUnit2==null) throw new PlatformException("操作失败!");
                begin = goodsUnit2.getSort();
            }
            List<GoodsUnit> goodsTypeList = unitDao.findBySort(begin,goodsUnit.getSort(),loginUser.getCompanyId());
            for(GoodsUnit t:goodsTypeList){
                if(t.getId().longValue() != goodsUnit.getId().longValue()){
                    t.setSort(t.getSort()+1);
                }else{
                    t.setSort(begin);
                }
            }
            unitDao.saveAll(goodsTypeList);
        }
        return true;
    }
}
